package com.wwz.util;

import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.wwz.pojo.FileInfo;
import com.wwz.pojo.model.LoginModel;
import com.wwz.service.IFileService;
import lombok.SneakyThrows;
import org.springframework.context.ApplicationContext;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.UUID;

/**
 * 项目工具类
 * @date 2022/2/16 15:55
 */
public class JobUtils {

    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static void setApplicationContext(ApplicationContext applicationContext) {
        JobUtils.applicationContext = applicationContext;
    }


    /**
     * 获取存储文件路径
     * @return
     */
    public static String getDirPath(){
        File file1 = new File(".files");

        //返回绝对路径
        return file1.getAbsolutePath();
    }

    /**
     * 下载文件
     * @param response
     * @param file
     */
    public static  void download(HttpServletResponse response, File file){
        try {
            //获取文件流
            FileInputStream fileInputStream = new FileInputStream(file);
            //文件大小
            int available = fileInputStream.available();
            //初始化缓存
            byte[] bytes = new byte[available];
            //写入缓存中
            fileInputStream.read(bytes);
            //获取文件名
            String name = file.getName();
            //获取文件真实名字
            String fileName = name.split("_")[1];
            //设置传输文件类型 和文件名
            response.setHeader("content-disposition","attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
            //获取response 输入流
            ServletOutputStream outputStream = response.getOutputStream();
            //写入数据
            outputStream.write(bytes);
            //关闭流
            outputStream.close();
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 上传文件
     * @param file
     * @return
     */
    @SneakyThrows
    public static String uploadFile(MultipartFile file){
        //获取文件名
        String name = file.getOriginalFilename();
        //将所有下划线替换成- 以防影响真实名切割
        name = name.replace("_", "-");
        //获取文件大小
        long size = file.getSize();
        //转换成M(兆)
        double m = (double)size/1024/1024;
        //文件后缀
        String suffix = FileUtil.getSuffix(name);
        //是图片
        if ("png".equalsIgnoreCase(suffix)||"jpg".equalsIgnoreCase(suffix)){
            //超过10M 不能上传
            if (m > 10){
                return "";
            }
        }
        //超过200M不能上传
        if (m > 200){
            return "";
        }
        //设置一个不重复的文件名
        String fileName =  UUID.randomUUID()+"_" + name;
        //获取输出流
        InputStream inputStream = file.getInputStream();
        int available = inputStream.available();
        //缓存
        byte[] bytes = new byte[available];
        //读
        inputStream.read(bytes);
        //获取存放文件的目录路径
        String filePath = getDirPath();
        //生成一个不重名的地址
        String path = filePath +File.separator+fileName;
        System.out.println("path = " + path);
        //获取输入流
        FileOutputStream fileOutputStream= new FileOutputStream(new File(path));
        //写
        fileOutputStream.write(bytes);
        //刷新
        fileOutputStream.flush();
        fileOutputStream.close();
        inputStream.close();

        return fileName;
    }

    /**
     * 获取文件
     * @return
     */
    public static File getFile(FileInfo fileInfo){
        String fileName = fileInfo.getName();
        String dirPath = JobUtils.getDirPath();
        //文件路径
        String filePath = dirPath +File.separator + fileName;
        File file = new File(filePath);
        return file;
    }

    /**
     * 创建验证码
     * @param response
     */
    public static void createImaCode(HttpServletResponse response){
        SaSession tokenSession = StpUtil.getTokenSession();
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
        try {
            lineCaptcha.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        tokenSession.set("img",lineCaptcha);
        //设置一个创建时间
        tokenSession.setCreateTime(new DateTime().getTime());
    }


    /**
     * 获取验证码code
     * @return
     */
    public static String getImgCode(){
        SaSession tokenSession = StpUtil.getTokenSession();
        //获取验证码类
        LineCaptcha imgCode = (LineCaptcha) tokenSession.get("img");

        //获取验证码创建时间
        long createTime = tokenSession.getCreateTime();
        //当前时间
        long now = new DateTime().getTime();
        //时间大于1分钟
        if (imgCode == null){
            throw new EcoBootException("验证码过期！");
        }

        //返回验证码code
        return imgCode.getCode();
    }

    /**
     * 登录效验
     */
    public static void checkLoginModel(LoginModel loginModel){

        if(loginModel ==null){
            throw new EcoBootException("登录失败!");
        }

        //验证 验证码
        //checkImgCode(loginModel.getTxt());

        if (StrUtil.isEmpty(loginModel.getUserName())||StrUtil.isEmpty(loginModel.getPassWord())){
            throw new EcoBootException("用户名密码为空!");
        }
    }

    /**
     * 登录效验
     */
    public static void checkImgCode(String txt){
        //获取验证码
        String code = JobUtils.getImgCode();

        if (!code.equalsIgnoreCase(txt)){
            throw new EcoBootException("验证码错误!");
        }
    }


    public static String getLogin(){
        return StpUtil.getLoginId().toString();
    }


    /**
     * 邮箱格式验证
     * @param s
     * @return
     */
    public static Boolean isEmail(String s){
        String reg="^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$";

        return  s.matches(reg);
    }


}
